const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-');
}

const getfutureDate = (number) => {
  const num = number ? number : 0;
  const date1 = new Date();
  //const time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate();//time1表示当前时间
  const date2 = new Date(date1);
  date2.setDate(date1.getDate() + num);
  const time2 = date2.getFullYear() + "-" + formatNumber(date2.getMonth() + 1) + "-" + formatNumber(date2.getDate());
  return time2;
};

 const getMonthDay = date => {
  const newDate = date.substring(5, 10).replace(/\//g, '-');
  return newDate;
}
const getYearMonthDay = date => {
  const newDate = date.substring(0, 10);
  return newDate;
}
//获取时间11-2
const getDateMountDay = (str) => {
  return str.replace(/\d{4}\/(\d{2}\/\d{2})/gi, function ($0, $1) {
    return $1
  })

}
//随机数不重复
const guid = () => {
  function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  }
  return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());

}

const goNextPage = str => {
  let token = wx.getStorageSync("token");
  if (!str) {
    return
  }
  if (token && token != "" && token != undefined) {
    wx.navigateTo({
      url: str
    });
  } else {
    wx.navigateTo({
      url: `/pages/my/login/login?redirect=${str.replace(/\.\.\//g, '')}`
    });
  }
}
module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  getfutureDate: getfutureDate,
  getMonthDay: getMonthDay,
  getYearMonthDay: getYearMonthDay,
  getDateMountDay: getDateMountDay,
  guid: guid,
  goNextPage:goNextPage
};
